package com.xx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xx.mapper.ShipDataMapper;
import com.xx.service.ShipDataService;
import com.xx.system.domain.data.ShipData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

/**
 * TODO
 *
 * @Author kzw
 * @Date 2025/3/27 14:33
 */
@Service
@RequiredArgsConstructor
public class ShipDataServiceImpl implements ShipDataService {

    // 注入船舶数据Mapper
    private final ShipDataMapper shipDataMapper;

    /**
     * 保存船舶数据
     * 设置创建时间和更新时间为当前时间，然后插入数据库
     *
     * @param shipData 要保存的船舶数据
     */
    @Override
    public void saveShipData(ShipData shipData) {
        shipData.setCreateTime(LocalDateTime.now());
        shipData.setUpdateTime(LocalDateTime.now());
        shipDataMapper.insert(shipData);
    }

    /**
     * 获取实时船舶数据
     * 按时间降序查询所有船舶数据，以获取最新的数据
     *
     * @return 船舶数据列表
     */
    @Override
    public List<ShipData> getRealtimeShipData() {
        // 直接调用Mapper中的自定义方法获取最新数据
        List<ShipData> shipDataList = shipDataMapper.selectLatestDataForEachShip();

        // 处理视频流地址
        for (ShipData shipData : shipDataList) {
            if ("412209510".equals(shipData.getMmsi())) {
                List<String> videoUrls = Arrays.asList(
                        "ws://116.148.231.66:10000/proxy/sms/local/live/34020000001110000001_34020000001310000004.flv",
                        "ws://116.148.231.66:10000/proxy/sms/local/live/34020000001110000001_34020000001310000006.flv",
                        "ws://116.148.231.66:10000/proxy/sms/local/live/34020000001110000001_34020000001310000009.flv"
                );
                shipData.setVideoUrls(videoUrls);
            }
        }

        return shipDataList;
    }

    /**
     * 根据船名或MMSI获取最新的船舶数据
     * 允许模糊查询船名或精确匹配MMSI，并返回时间戳最新的那条数据
     *
     * @param shipName 船名，用于模糊查询
     * @param mmsi     MMSI，用于精确匹配
     * @return 最新的船舶数据，如果没有则返回null
     */
    @Override
    public ShipData getLatestShipDataByNameOrMmsi(String shipName, String mmsi) {
        LambdaQueryWrapper<ShipData> wrapper = new LambdaQueryWrapper<>();

        // 构建查询条件：船名模糊查询或MMSI精确匹配
        wrapper.and(w -> w
                .like(shipName != null && !shipName.trim().isEmpty(), ShipData::getName, shipName)
                .or()
                .eq(mmsi != null, ShipData::getMmsi, mmsi)
        );

        // 按时间戳降序排序并限制返回一条
        wrapper.orderByDesc(ShipData::getTime)
                .last("LIMIT 1");

        ShipData shipData = shipDataMapper.selectOne(wrapper);

        // 如果查询到数据，转换时间格式
        if (shipData != null && shipData.getTime() != null) {
            // 将时间戳转换为LocalDateTime
            LocalDateTime dateTime = LocalDateTime.ofEpochSecond(
                    shipData.getTime(),
                    0,
                    ZoneOffset.ofHours(8) // 使用东八区
            );

            // 格式化时间
            shipData.setTimeStr(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }

        return shipData;
    }
}
